var token = selectByKey("like_token");
function s(select) {
    return document.querySelector(select);
}
//充值
function recharge(dom) {
    var money = dom.innerHTML;
    $.ajax({
        url: pathName + '/api/v1/pay/recharge',
        type: 'post',
        data: {
            "payment_id": 1,
            "amount": money
        },
        headers: {
            "Accept": "application/json",
            "Authorization": "Bearer " + token
        },
        success: function (res) {
            window.location.href = res.success;
        }
    })
}
//改变金额
function  changeNum (status,dom,type) {
    var nowNum,
        aftNum;
    type == 1 ? nowNum = dom.innerHTML : nowNum = dom.value;
    if(status == 1){
        aftNum = Math.floor(nowNum) + 1
    }else{
        if(nowNum <= 100){
            showInfo();
            return;
        }
        aftNum = nowNum - 1;
    }
    type == 1 ? dom.innerHTML = aftNum : dom.value = aftNum;
}
//显示提示信息
function showInfo() {
    var infoNode = s('.info');
    infoNode.style.display = 'block';
    setTimeout(function () {
        infoNode.style.opacity = 1;
    },50);
    setTimeout(function () {
        infoNode.style.opacity = 0;
    },1500);
    setTimeout(function () {
        infoNode.style.display = 'none';
    },2000);
}
//显示或关闭充值弹框
function switchLayer(statu) {
    var layer = s('.mark');
    if(statu == 1){
        layer.style.display = 'block';
        setTimeout(function () {
            layer.style.opacity = 1;
        },50);
    }else{
        layer.style.opacity = 0;
        setTimeout(function () {
            layer.style.display = 'none';
        },500);
    }
}
//改变金额
function switchNum(statu) {
    var moneyNode = s('.box span'),
        moneyBoxNode = s('.markBox input');
    if(statu == 1){
        moneyBoxNode.value = moneyNode.innerHTML;
    }else{
        if(moneyBoxNode.value < 100){
            moneyNode.innerText = 100;
            showInfo();
            return;
        }
        moneyNode.innerText = moneyBoxNode.value;
    }
}
window.onload = function () {
    var submit = s('footer'),
        cutNode =  s('.box i'),
        addNode = s('.box em'),
        moneyNode = s('.box span'),
        cutBoxNode = s('.markBox i'),
        addBoxNode = s('.markBox s'),
        moneyBoxNode = s('.markBox input'),
        sureNode = s('#sure'),
        cancleNode = s('#cancle');
    Tap(cutNode,function () {
        changeNum(-1,moneyNode,1);
    });
    Tap(addNode,function () {
        changeNum(1,moneyNode,1);
    });
    Tap(cutBoxNode,function () {
        changeNum(-1, moneyBoxNode,2);
    });
    Tap(addBoxNode, function () {
        changeNum(1, moneyBoxNode,2);
    })
    Tap(moneyNode,function () {
        switchLayer(1);
        switchNum(1);
    });
    Tap(cancleNode,function () {
        switchLayer(-1);
    })
    Tap(sureNode,function () {
        switchLayer(-1);
        switchNum(-1);
    })
    Tap(submit,function () {
        recharge(moneyNode);
    });
}